package com.seng.resourcetycoon.services;

import com.seng.resourcetycoon.core.enums.ResourceType;
import com.seng.resourcetycoon.event.ResourceCountChangeEvent;
import javafx.beans.value.ChangeListener;

import java.util.ArrayList;
import java.util.List;

public class ResourceInventory  {

    private final List<ChangeListener<ResourceCountChangeEvent>> eventListeners = new ArrayList<>();

    private static final ResourceInventory resourceInventory = new ResourceInventory();

    public static ResourceInventory getInstance() {
        return resourceInventory;
    }

    private ResourceInventory() {

    }

    public void setResourceCount(ResourceType resourceType, int value) {
        fireEvent(new ResourceCountChangeEvent(resourceType,  value));
    }

    public void addResourceCountChangeListener(ChangeListener<ResourceCountChangeEvent> listener) {
        eventListeners.add(listener);
    }

    public void removeResourceCountChangeListener(ChangeListener<ResourceCountChangeEvent> listener) {
        eventListeners.remove(listener);
    }

    private void fireEvent(ResourceCountChangeEvent event) {
        eventListeners.forEach(listener -> listener.changed(null, null, event));
    }
}
